package com.cloud.DrugManagement.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.DrugManagement.enums.OrderState;
import com.cloud.DrugManagement.mapper.OrderContentMapper;
import com.cloud.DrugManagement.mapper.OrderDetailsMapper;
import com.cloud.DrugManagement.mapper.ReceiptHeaderMapper;
import com.cloud.DrugManagement.pojo.OrderDetails;
import com.cloud.DrugManagement.service.OrderDetailsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author zhozixiong
 * @date 2023/02/11
 */
@Service
@RequiredArgsConstructor
public class OrderDetailsServiceImpl extends ServiceImpl<OrderDetailsMapper, OrderDetails> implements OrderDetailsService {

    private final OrderContentMapper orderContentMapper;
    private final ReceiptHeaderMapper receiptHeaderMapper;

    private final static int SIZE = 10;

    @Override
    public boolean insert(OrderDetails order) {
        return baseMapper.insert(order) >= 1;
    }

    @Override
    public boolean updateStateByOid(OrderState orderState, Long oid) {
        return baseMapper.updateStateByOid(orderState, oid) >= 1;
    }

    @Override
    public boolean updateCreationTimeStateByOid(Long oid) {
        receiptHeaderMapper.insert(oid);
        return baseMapper.updateCreationTimeStateByOid(new Date(), OrderState.SUBMITTED, oid) >= 1;
    }

    @Override
    public boolean delByOid(long oid) {
        //删除订单内容
        orderContentMapper.delByOid(oid);
        //删除订单
        baseMapper.delByOid(oid);
        return true;
    }

    @Override
    public OrderDetails searchAllByOid(long oid) {
        return baseMapper.searchAllByOid(oid);
    }

    @Override
    public Page<OrderDetails> searchAllOrOid(int pages, Long oid) {
        Page page = new Page(pages, SIZE);
        return baseMapper.searchAllOrOid(page, oid);
    }
}
